package com.dmz.wxcp.wecom.wxbusiness.controller;

import com.dmz.wxcp.utils.DataResult;
import com.dmz.wxcp.vo.req.TransportReqVO;
import com.dmz.wxcp.vo.resp.PageRespVO;
import com.dmz.wxcp.wecom.wxbusiness.entity.ArticleTags;
import com.dmz.wxcp.wecom.wxbusiness.entity.Transport;
import com.dmz.wxcp.wecom.wxbusiness.mapper.TransportMapper;
import com.dmz.wxcp.wecom.wxbusiness.service.TransportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/transport")
public class TransportController {

    @Autowired
    private TransportMapper transportMapper;

    @Autowired
    private TransportService transportService;

    @PostMapping("/getlist")
    @ResponseBody
    public DataResult getTransportList(@RequestParam Map<String,Object> map){
        TransportReqVO vo=new TransportReqVO();
        vo.setSearch((map.get("search").toString()));
        vo.setTag((map.get("tag").toString()));
        vo.setCurrentPage(new Long(map.get("page").toString()));
        vo.setPageSize(new Long(map.get("limit").toString()));
        vo.setStartNum((vo.getCurrentPage()-1)*vo.getPageSize());


        List<Object> result=transportMapper.getTransportList(vo);
        List<Transport> transports=(List<Transport>)result.get(0);
        Long count= new Long(((List<Integer>) result.get(1)).get(0));

        PageRespVO<Transport> respVO=new PageRespVO<>();
        respVO.setRecords(transports);
        respVO.setTotal(count);
//        获取页数  总数除以每页数量
        Long size=count % vo.getPageSize() == 0 ? count/vo.getPageSize() :count/vo.getPageSize()+1;
        respVO.setSize(size);

        return DataResult.success(respVO);
    }

    @GetMapping("/transportindex")
    public String transportIndex(Model model){
       String[] provinceArray=new String[]{
       "北京","天津","河北省","山西省","内蒙古自治区","辽宁省","吉林省","黑龙江省","上海",
               "江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省",
               "广东省","广西壮族自治区","海南省","重庆","四川省","贵州省","云南省","西藏自治区",
               "陕西省","甘肃省","青海省","宁夏回族自治区","新疆维吾尔自治区","台湾","香港特别行政区",
               "澳门特别行政区"
       };

        List<ArticleTags> hottags=transportService.getTransportHotTags();
        model.addAttribute("provinces",provinceArray);
        model.addAttribute("hottags",hottags);

        return "transportindex";
    }

    @GetMapping("/getdetail")
    public String transportDetail(@RequestParam("id") String id, Model model){
        Transport t=transportMapper.selectById(id);
        model.addAttribute("transport",t);
        return "transportdetail";
    }
}
